package com.ruoyi.web.controller.base;

import com.ruoyi.base.domain.BaseRegion;
import com.ruoyi.base.domain.RegionData;
import com.ruoyi.base.service.BaseRegionService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/base/region")
public class BaseRegionController extends BaseController {

    @Autowired
    private BaseRegionService baseRegionService;
    @Autowired
    private RuoYiConfig ruoYiConfig;

    /**
     * 区划列表 排除节点
     * @param region
     * @return
     */
    @GetMapping("/list/exclude/node")
    public AjaxResult excludeChild(BaseRegion region)
    {
        List<BaseRegion> depts = baseRegionService.selectRegionListNew(region);
        depts.removeIf(d -> d.getCode().equals(region.getCode()));
        return AjaxResult.success(depts);
    }

    /**
     * 查询行政区划列表
     */
    @GetMapping("/selectRegionList")
    public TableDataInfo selectRegionList(BaseRegion pubCodeRegion)
    {
        List<RegionData> list = baseRegionService.selectRegionList(pubCodeRegion);
        return getDataTable(list);
    }

    /**
     * 查询行政区划列表 树形结构
     */
    @GetMapping("/selectRegionListNew")
    public TableDataInfo selectRegionListNew(BaseRegion pubCodeRegion)
    {
        String code = pubCodeRegion.getParentCode();
        if(code!=null&&!"".equals(code)){

        }else {
            String countyCode = ruoYiConfig.getCountyCode();
            pubCodeRegion.setParentCode(countyCode);
        }
        List<BaseRegion> list = baseRegionService.selectRegionListNew(pubCodeRegion);
        return getDataTable(list);
    }
    /**
     * 查询行政区划列表 树形结构
     */
    @GetMapping("/getRegionList")
    public TableDataInfo getRegionList(BaseRegion pubCodeRegion)
    {
        String code = pubCodeRegion.getParentCode();
        if(code!=null&&!"".equals(code)){

        }else {
            String countyCode = ruoYiConfig.getCountyCode();
            pubCodeRegion.setParentCode(countyCode);
        }
        List<BaseRegion> list = baseRegionService.getRegionList(pubCodeRegion);
        return getDataTable(list);
    }
    /**
     * 查询行政区划列表
     */

    @GetMapping("/list")
    public TableDataInfo list(BaseRegion pubCodeRegion)
    {
        startPage();
        List<BaseRegion> list = baseRegionService.selectPubCodeRegionList(pubCodeRegion);
        return getDataTable(list);
    }

    /**
     * 导出行政区划列表
     */

    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseRegion pubCodeRegion)
    {
        List<BaseRegion> list = baseRegionService.selectPubCodeRegionList(pubCodeRegion);
        ExcelUtil<BaseRegion> util = new ExcelUtil<BaseRegion>(BaseRegion.class);
        util.exportExcel(response, list, "行政区划数据");
    }

    /**
     * @Description //TODO 行政区划根据excel导入
     * @Param [file, sheetName]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/

    @RequestMapping("/importpucode")
    public Map<String,Object> importpucode(MultipartFile file, String sheetName)
    {
        Map<String,Object> maplist=new HashMap<>();
        String fileName = file.getOriginalFilename();
        if (!fileName.toLowerCase().endsWith(".xls") && !fileName.toLowerCase().endsWith(".xlsx")) {
            maplist.put("msg", "上传文件格式错误");
            maplist.put("code", 500);
            maplist.put("data", "");
        } else {
            Map<String, Object> list = baseRegionService.importpucodeExcel(file, sheetName);
            maplist.put("msg", "查询成功");
            maplist.put("code", 200);
            maplist.put("data", list);
            return list;

        }
        return  maplist;
    }
    /**
     * 获取行政区划详细信息
     */

    @GetMapping(value = "/getinfo/{code}")
    public AjaxResult getInfo(@PathVariable("code") String code)
    {
        return success(baseRegionService.selectPubCodeRegionByCode(code));
    }

    /**
     * 新增行政区划
     */

    @PostMapping("/add")
    public AjaxResult add(@RequestBody BaseRegion pubCodeRegion)
    {
        return toAjax(baseRegionService.insertPubCodeRegion(pubCodeRegion));
    }

    /**
     * 修改行政区划
     */
//    @PreAuthorize("@ss.hasPermi('base:region:edit')")
//    @Log(title = "行政区划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BaseRegion pubCodeRegion)
    {
        return toAjax(baseRegionService.updatePubCodeRegion(pubCodeRegion));
    }

    /**
     * 删除行政区划
     */
//    @PreAuthorize("@ss.hasPermi('base:region:remove')")
//    @Log(title = "行政区划", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{Codes}")
    public AjaxResult remove(@PathVariable String[] Codes)
    {
        return toAjax(baseRegionService.deletePubCodeRegionByCodes(Codes));
    }

}
